package cn.flant.log.adapter;

public class Log4jAdapter extends LogAdapter {

	private org.apache.log4j.Logger logger = null;

	protected Log4jAdapter(String name) {
		super(name);
		logger = org.apache.log4j.Logger.getLogger(name);
		org.apache.log4j.Category temp = logger;
		boolean hasAppender = false;
		while (temp != null) {
			hasAppender = temp.getAllAppenders().hasMoreElements();
			if (hasAppender) {
				break;
			} else {
				temp = temp.getParent();
			}
		}
		if (!hasAppender) {
			throw new IllegalStateException("Log4j is not available");
		}
	}

	@Override
	public void debug(Object msg) {
		if (DEBUG < level) {
			return;
		}
		logger.debug(msg);
	}

	@Override
	public void debug(Object msg, Throwable t) {
		if (DEBUG < level) {
			return;
		}
		logger.debug(msg, t);
	}

	@Override
	public void error(Object msg) {
		if (ERROR < level) {
			return;
		}
		logger.error(msg);
	}

	@Override
	public void error(Object msg, Throwable t) {
		if (ERROR < level) {
			return;
		}
		logger.error(msg, t);
	}

	@Override
	public void fatal(Object msg) {
		if (FATAL < level) {
			return;
		}
		logger.fatal(msg);
	}

	@Override
	public void fatal(Object msg, Throwable t) {
		if (FATAL < level) {
			return;
		}
		logger.fatal(msg, t);
	}

	@Override
	public void info(Object msg) {
		if (INFO < level) {
			return;
		}
		logger.info(msg);
	}

	@Override
	public void info(Object msg, Throwable t) {
		if (INFO < level) {
			return;
		}
		logger.info(msg, t);
	}

	@Override
	public void warn(Object msg) {
		if (WARN < level) {
			return;
		}
		logger.warn(msg);
	}

	@Override
	public void warn(Object msg, Throwable t) {
		if (WARN < level) {
			return;
		}
		logger.warn(msg, t);
	}
}
